<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">


      <el-form-item label="产地" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入产地"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批号" prop="batchnumbe">
        <el-input
          v-model="queryParams.batchnumbe"
          placeholder="请输入批号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="摘要" prop="abstractr">
        <el-input
          v-model="queryParams.abstractr"
          placeholder="请输入摘要"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="填制人" prop="filledby">
        <el-input
          v-model="queryParams.filledby"
          placeholder="请输入填制人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数量" prop="no">
        <el-input
          v-model="queryParams.no"
          placeholder="请输入数量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:purchase:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:purchase:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:purchase:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:purchase:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="purchaseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="药品采购信息Id" align="center" prop="purchaseid" />
      <el-table-column label="药品名称" align="center" prop="name" />
      <el-table-column label="药品来源" align="center" prop="source" />
      <el-table-column label="产地" align="center" prop="address" />
      <el-table-column label="批号" align="center" prop="batchnumbe" />
      <el-table-column label="生产日期" align="center" prop="manufacturetime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.manufacturetime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效期" align="center" prop="termofvalidity" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.termofvalidity, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="摘要" align="center" prop="abstractr" />
      <el-table-column label="填制人" align="center" prop="filledby" />
      <el-table-column label="核查人" align="center" prop="verifier" />
      <el-table-column label="核查日期" align="center" prop="verificationtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.verificationtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      
      <el-table-column label="数量" align="center" prop="no" />
      <el-table-column label="状态" align="center" prop="status">
      	     <template scope="scope">
        <span v-if="scope.row.status=='1'"  class="el-tag el-tag--medium el-tag--light" index="0"> 未审查 </span>
         <span v-if="scope.row.status=='2'"  class="el-tag el-tag--medium el-tag--light" index="0"> 已入库 </span>
          <span v-if="scope.row.status=='3'"  class="el-tag el-tag--medium el-tag--light" index="0"> 未核查 </span>
           <span v-if="scope.row.status=='5'"  class="el-tag el-tag--medium el-tag--light" index="0"> 被驳回 </span>
    </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-success"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:purchase:edit']"
          >通过</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-error"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:purchase:edit']"
          >驳回</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改药品采购对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="药品名称" prop="drugsid">
         <!-- <el-input v-model="form.drugsid" placeholder="请输入药品名称ID" /> -->
          <el-select placeholder="请选择" v-model="form.drugsid" >
            <el-option v-for="s in drugs" :label="s.name" :value="s.drugsid" :key="s.drugsid"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="药品来源" prop="source">
          <el-input v-model="form.source" placeholder="请输入药品来源" />
        </el-form-item>
        <el-form-item label="产地" prop="address">
          <el-input v-model="form.address" placeholder="请输入产地" />
        </el-form-item>
        <el-form-item label="批号" prop="batchnumbe">
          <el-input v-model="form.batchnumbe" placeholder="请输入批号" />
        </el-form-item>
        <el-form-item label="生产日期" prop="manufacturetime">
          <el-date-picker clearable size="small"
            v-model="form.manufacturetime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择生产日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="有效期" prop="termofvalidity">
          <el-date-picker clearable size="small"
            v-model="form.termofvalidity"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择有效期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="摘要" prop="abstractr">
          <el-input v-model="form.abstractr" placeholder="请输入摘要" />
        </el-form-item>
        <el-form-item label="填制人" prop="filledby">
          <el-input v-model="form.filledby" placeholder="请输入填制人" :value="form.userName"/>
        </el-form-item>
        <el-form-item label="核查人" prop="verifier">
          <el-input v-model="form.verifier" placeholder="请输入核查人" />
        </el-form-item>
        <el-form-item label="核查日期" prop="verificationtime">
          <el-date-picker clearable size="small"
            v-model="form.verificationtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择核查日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核人" prop="reviewer">
          <el-input v-model="form.reviewer" placeholder="请输入审核人" />
        </el-form-item>
        <el-form-item label="审核日期" prop="audittime">
          <el-date-picker clearable size="small"
            v-model="form.audittime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择审核日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="数量" prop="no">
          <el-input v-model="form.no" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
        	<el-select v-model="form.status" placeholder="状态">
        			<el-option v-for="s in statusOptiones" :label="s.dictLabel" :value="s.dictValue" :key="s.dictValue" />
        	</el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPurchase, getPurchase, delPurchase, addPurchase, updatePurchase, exportPurchase } from "@/api/system/purchase";
import { listInfo,getInfo} from "@/api/system/info";
import { getUser,getUserProfile} from "@/api/system/user";
import { getStatus} from "@/api/system/status";
export default {
  name: "Purchase",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 药品采购表格数据
      purchaseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        drugsid: null,
        source: null,
        address: null,
        batchnumbe: null,
        manufacturetime: null,
        termofvalidity: null,
        abstractr: null,
        filledby: null,
        verifier: null,
        verificationtime: null,
        reviewer: null,
        audittime: null,
        no: null,
        status:null
      },
      // 表单参数
      form: {},
      drugs:null,
      statuss:null,
      user:{},
      // 表单校验
      rules: {
      },
      statusOptiones:[]
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_ds").then(response => {
    	  this.statusOptiones = response.data;
    	});
    listInfo('').then(response => {
        this.drugs = response.rows;
     });


  },
  methods: {
    /** 查询药品采购列表 */
    getList() {
      this.loading = true;
      this.queryParams.status = 3;
      listPurchase(this.queryParams).then(response => {
        this.purchaseList = response.rows;
        this.total = response.total;
        this.loading = false;
        console.log(response.rows);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
        purchaseid: null,
        drugsid: null,
        source: null,
        address: null,
        batchnumbe: null,
        manufacturetime: null,
        termofvalidity: null,
        abstractr: null,
        filledby: null,
        verifier: null,
        verificationtime: null,
        reviewer: null,
        audittime: null,
        no: null,
        status:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.purchaseid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      getUserProfile().then( response =>{
        this.form.filledby = response.data.userName;
        console.log(this.form.filledby);
      });
      this.open = true;
      this.title = "添加药品采购";
    },
    /**  */

    updateStatus(){
      updatePurchase(this.form).then(response => {
        this.$modal.msgSuccess("核查成功");
        this.getList();
      });
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const purchaseid = row.purchaseid || this.ids

      getPurchase(purchaseid).then(response => {
        this.form = response.data;
        this.form.status = 1;
        this.form.verifier = this.$store.state.user.name;
        // this.open = true;
        // this.title = "修改药品采购";
        this.updateStatus();

      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.purchaseid != null) {
            updatePurchase(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPurchase(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    deleteStatus(){
      updatePurchase(this.form).then(response => {
        this.$modal.msgSuccess("核查已驳回");
        this.getList();
      });
    },
    /** 驳回按钮操作 */
    handleDelete(row) {
      this.reset();
      const purchaseid = row.purchaseid || this.ids

      getPurchase(purchaseid).then(response => {
        this.form = response.data;
        this.form.status = 5;
        // this.open = true;
        // this.title = "修改药品采购";
        this.deleteStatus();
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有药品采购数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportPurchase(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
